package com.dz.server.controller;


import com.dz.server.pojo.Atomic;
import com.dz.server.pojo.Product;
import com.dz.server.pojo.ProductAtomic;
import com.dz.server.service.AtomicService;
import com.dz.server.service.ProductAtomicService;
import com.dz.server.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dz
 * @since 2022-03-23
 */
@Controller
public class AtomicController {
    @Autowired
    private AtomicService atomicService;
    @Autowired
    private ProductService productService;
    @Autowired
    private ProductAtomicService productAtomicService;

    private Product product;
    //去原子服务界面
    @RequestMapping("/toAtomic")
    public String toAtomicPage(Product product,Model model){
        this.product = product;
        List<Atomic> atomics = atomicService.list();
        model.addAttribute("atomics",atomics);
        return "server/atomicPage";
    }
    //发布产品
    @GetMapping("/publishProduct")
    public String publishProduct(String msg){
        productService.save(product);
        List<Atomic> atomics = atomicService.getAtomics(msg);
        for (Atomic atomic : atomics) {
            ProductAtomic productAtomic = new ProductAtomic(product.getId(), atomic.getId());
            productAtomicService.save(productAtomic);
        }
        return "redirect:/toIndex";
    }
}

